package com.pg.excel;

import com.pg.excel.bean.DidBean;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class WriteExcel {
    private static WriteExcel ourInstance = new WriteExcel();

    public static WriteExcel getInstance() {
        return ourInstance;
    }

    private WriteExcel() {
    }
    private XSSFCellStyle rateStyle = null;

    public boolean writeExcelDids(List<DidBean> list, String path){
        File file = new File(path);
        FileOutputStream out = null;
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            //新建表格
            Sheet sheet = workbook.createSheet();
            //生成表头
            Row thead = sheet.createRow(0);
            thead.createCell(0).setCellValue("did");
            thead.createCell(1).setCellValue("成功次数");
            thead.createCell(2).setCellValue("超时次数");
            thead.createCell(3).setCellValue("超时率");
            int rowNum = 1;
            for (DidBean bean :list){
//                System.out.println(bean.toString());
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(bean.getDid());
                row.createCell(1).setCellValue(bean.getSuccess());
                row.createCell(2).setCellValue(bean.getFail());
                Cell cell = row.createCell(3);
                if (rateStyle == null){
                    rateStyle = workbook.createCellStyle();
                    rateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
                    rateStyle.setAlignment(HorizontalAlignment.LEFT);
                }
                cell.setCellStyle(rateStyle);
                if (bean.getSuccess() > 0 && bean.getFail() > 0){
                    float fRate = bean.getFail()*1.0f/(bean.getFail()+bean.getSuccess());
                    cell.setCellValue(fRate);
                }else if (bean.getSuccess() > 0){
                    cell.setCellValue(0);
                }else{
                    cell.setCellValue(1);
                }
            }

            if (!file.exists()){
                file.createNewFile();
            }
            out = new FileOutputStream(file);
            // 把相应的Excel 工作簿存盘
            workbook.write(out);
            out.flush();
            out.close();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            rateStyle = null;
        }
        return true;
    }

}
